<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>银行信息</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.5.6/css/layui.css" media="all">
    <style>
        .bankInfo {
            padding: 20px;
        }

        td[data-field="imgUrl"] div {
            height: 120px;
        }

        .lay-form {
            padding: 20px;
        }
        .bankInfo {
            background-color: #fff;
        }
    </style>
</head>

<body>
    <div class="bankInfo">
        <table lay-filter="bankTable" class="layui-hide" id="bankTable"></table>
    </div>

</body>

<!-- 添加 -->
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</script>

<!-- 操作 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="https://cdn.bootcdn.net/ajax/libs/layui/2.5.7/layui.all.min.js"></script>
<script src="../utils.js"></script>
<script>
    layui.use(['table', 'upload', 'form', 'jquery'], function () {
        var table = layui.table,
            upload = layui.upload,
            form = layui.form,
            $ = layui.jquery;

        table.render({
            elem: '#bankTable'
            , toolbar: "#toolbarDemo"
            , url: window.baseUrl + '/indexUser/getBankInfo'
            , headers: {
                Authorization: localStorage.token
            }
            , response: {
                statusCode: 200 //规定成功的状态码，默认：0
            }
            , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            , cols: [[
                { field: 'bankId', width: 80, title: 'ID', sort: true }
                , { field: 'bankName', title: '名称' }
                , {
                    field: 'bankImg', title: '图片', templet: function (res) {
                        return '<img style="width:100%; height:100%; object-fit: contain; max-width:initial;" src="' + res.bankImg + '" />'
                    }
                }
                , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150 }
            ]]
        });

        //工具栏事件
        table.on('toolbar(bankTable)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    $("#bankId").val("");
                    $("#imgUrl").val("");
                    //页面层
                    showLayer();
                    break;
            };
        });

        // 编辑和删除
        table.on("tool(bankTable)", function(data) {
            switch(data.event) {
                case "del":
                    layer.confirm('确定删除？', function(index){
                        ajax({
                            path: "/indexUser/delBank?bankId=" + data.data.bankId,
                            type: 'GET',
                            success: function (res) {
                                layer.closeAll();
                                layer.msg("删除成功");
                                table.reload("bankTable");
                            }
                        }, $)
                    });
                    break;
                case "edit":
                    showLayer();
                    form.val('bankForm', data.data);
                    $('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', data.data.bankImg);
                    break;
            }
        })


        function showLayer() {
            layer.open({
                type: 1,
                title: "添加银行信息",
                area: ['650px', '600px'], //宽高
                content: `
                            <form class="layui-form lay-form" lay-filter="bankForm">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">银行名称</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="bankName" lay-verify="required" lay-reqtext="银行名称是必填项，岂能为空？" placeholder="请输入名称" autocomplete="off" class="layui-input">
                                    </div>
                                </div>

                                <input type="hidden" name="bankImg" id="imgUrl"/>
                                <input type="hidden" name="bankId" id="bankId"/>

                                <div class="layui-form-item">
                                    <label class="layui-form-label">图片上传</label>
                                    <div class="layui-upload-drag" id="uploadimg">
                                        <i class="layui-icon"></i>
                                        <p>点击上传，或将文件拖拽到此处</p>
                                        <div class="layui-hide" id="uploadDemoView">
                                            <hr>
                                            <img src="" alt="上传成功后渲染" style="max-width: 196px">
                                        </div>
                                    </div>
                                </div>
                                
                                
                                <div class="layui-form-item">
                                    <div class="layui-input-block">
                                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="addBankSubmit">立即提交</button>
                                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                    </div>
                                </div>
                            </form>
                        `
            });

            uploads(upload, '#uploadimg');
            // form 提交
            form.on("submit(addBankSubmit)", function (el) {
                delete el.field.file;

                if(el.field.bankId) {
                    ajax({
                        path: "/indexUser/editBank",
                        data: el.field,
                        success: function (res) {
                            layer.closeAll();
                            layer.msg("编辑成功");
                            table.reload("bankTable");
                        }
                    }, $)
                } else {
                    ajax({
                        path: "/indexUser/addBankInfo",
                        data: el.field,
                        success: function (res) {
                            layer.closeAll();
                            layer.msg("添加成功");
                            table.reload("bankTable");
                        }
                    }, $)
                }
                
                

                return false;
            })
        }
    });
</script>

</html>